﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TsiU;

namespace GCommon
{
    public interface ISingletonModule
    {
        void Init();
        void Cleanup();
    }
    public abstract class SingletonModule<T> : ISingletonModule where T : SingletonModule<T>, new()
    {
        private static T _instance = default(T);
        public static T instance
        {
            get
            {
                if (_instance == null)
                {
                    _instance = new T();
                    _instance.Init();
                }
                return _instance;
            }
        }
        private bool m_HasInited = false;
        private bool m_HasUnloadPartialMemory = false;
        public SingletonModule()
        {
            SingletonModuleAppContext.RegisterModule(this);
        }
        public void Init()
        {
            if(m_HasInited == false)
            {
                OnInit();
                m_HasInited = true;
            }
        }
        public void Cleanup()
        {
            OnCleanup();
            m_HasInited = false;
        }
        protected abstract void OnInit();
        protected abstract void OnCleanup();

        public void UnLoadMemoryIfNecessary()
        {
            OnUnloadMemeory();
            m_HasUnloadPartialMemory = true;
        }

        public void ReloadMemoryIfNecessary()
        {
            if (m_HasUnloadPartialMemory)
            {
                OnReloadMemory();
                m_HasUnloadPartialMemory = false;
            }
        }

        protected virtual void OnUnloadMemeory()
        {
            
        }

        protected virtual void OnReloadMemory()
        {
            
        }
    }
}
